package com.skirmisher.inspection.rectify.service.Impl;

import com.skirmisher.inspection.common.entity.InsFile;
import com.skirmisher.inspection.common.entity.InsUserEntity;
import com.skirmisher.inspection.common.mapper.InsFileMapper;
import com.skirmisher.inspection.common.service.SystemCodeCacheService;
import com.skirmisher.inspection.common.sysenum.InsJobStatusEnum;
import com.skirmisher.inspection.conform.entity.InsConformJob;
import com.skirmisher.inspection.conform.mapper.InsConformJobMapper;
import com.skirmisher.inspection.daily.entity.InsDailyJob;
import com.skirmisher.inspection.daily.mapper.InsDailyJobMapper;
import com.skirmisher.inspection.rectify.entity.*;
import com.skirmisher.inspection.rectify.mapper.InsJobErrotMapper;
import com.skirmisher.inspection.rectify.mapper.InsJobRectifyDetailMapper;
import com.skirmisher.inspection.rectify.mapper.InsJobRectifyMapper;
import com.skirmisher.inspection.rectify.service.InsJobRectifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName InsJobRectifyServiceImpl
 * @Description TODO
 * @Author hanwangxin
 * @Date 2020/12/14 下午9:38
 * @Version 1.0
 */
@Service
@Transactional
@Slf4j
public class InsJobRectifyServiceImpl implements InsJobRectifyService {

    @Autowired
    private InsJobRectifyMapper insJobRectifyMapper;
    @Autowired
    private InsJobRectifyDetailMapper insJobRectifyDetailMapper;
    @Autowired
    private InsDailyJobMapper insDailyJobMapper;

    @Autowired
    private InsConformJobMapper insConformJobMapper;
    @Autowired
    private InsJobErrotMapper insJobErrotMapper;
    @Autowired
    private InsFileMapper insFileMapper;



    @Autowired
    private SystemCodeCacheService systemCodeCacheService;

    @Override
    public void rectifyPageQuery(InsJobRectifyPageQuery query) {

        List<Object> list = insJobRectifyMapper.rectifyPageList(query);
        int count = insJobRectifyMapper.rectifyPageCount(query);
        query.setDateList(list);
        query.setCount(count);
    }

    @Override
    public InsJobRectify rectifyDetail(String rectifyId) {

        InsJobRectify rectify = insJobRectifyMapper.getById(rectifyId);
        if (rectify == null){
            return rectify;
        }
        List<InsJobRectifyDetail> details = insJobRectifyDetailMapper.findByRectityId(rectifyId);
        for (InsJobRectifyDetail detail : details) {
            String id = detail.getId();
            List<InsFile> fileList = insFileMapper.getFileList(id);
            detail.setFiles(fileList);
        }
        rectify.setDetails(details);
        return rectify;
    }

    @Override
    public void checkRectifyDetail(InsUserEntity user) {

        String bindId = user.getBindId();
        //更新整改任务状态
        InsJobRectify rectify = insJobRectifyMapper.getById(bindId);
        if (rectify == null){
            return;
        }
        InsJobRectify updateRectify = new InsJobRectify();
        updateRectify.setId(bindId);
        updateRectify.setRectifyCheckCode(user.getWorkCode());
        updateRectify.setRectifyCheckName(user.getWorkName());
        updateRectify.setRectifyCheckTime(new Date());
        updateRectify.setRectifyStatus("4");
        insJobRectifyMapper.updateIgnoreNull(updateRectify);
        //更新异常信息
        InsJobError updateError = new InsJobError();
        updateError.setJobRctifyId(bindId);
        updateError.setRectifyCheckCode(user.getWorkCode());
        updateError.setRectifyCheckName(user.getWorkName());
        updateError.setRectifyCheckTime(new Date());
        updateError.setRectifyStatus("4");
        insJobErrotMapper.updateIgnoreNull(updateError);
        //更新所绑定的任务状态
        String jobType = rectify.getJobType();
        String jobId = rectify.getJobId();
        if ("daily".equals(jobType)){
            InsDailyJob daliyJob = new InsDailyJob();
            daliyJob.setId(jobId);
            daliyJob.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");

            insDailyJobMapper.updateIgnoreNull(daliyJob);
        }else {
            InsConformJob conformJob = new InsConformJob();
            conformJob.setId(jobId);
            conformJob.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");
            insConformJobMapper.updateIgnoreNull(conformJob);
        }
    }

    @Override
    public void sendInsRectifyDetail(InsUserEntity user) {

        String bindId = user.getBindId();
        //更新整改任务状态
        InsJobRectify updateRectify = new InsJobRectify();
        updateRectify.setId(bindId);
        updateRectify.setRectifyUpdateCode(user.getWorkCode());
        updateRectify.setRectifyUpdateName(user.getWorkName());
        updateRectify.setRectifyStatus("2");
        insJobRectifyMapper.updateIgnoreNull(updateRectify);

        //更新异常信息
        InsJobError updateError = new InsJobError();
        updateError.setJobRctifyId(bindId);
        updateError.setRectifyCheckCode(user.getWorkCode());
        updateError.setRectifyCheckName(user.getWorkName());
        updateError.setRectifyCheckTime(new Date());
        updateError.setRectifyStatus("2");
        insJobErrotMapper.updateIgnoreNull(updateError);
    }

    @Override
    public String saveInsRectify(InsJobRectifySave save) {

        InsJobRectify rectify = new InsJobRectify();
        BeanUtils.copyProperties(save, rectify);
        rectify.setRectifyRecTime(new Date());
        String rectifyId = UUID.randomUUID().toString();
        rectify.setId(rectifyId);

        String nextCode = systemCodeCacheService.getNextCode(SystemCodeCacheService.RECTIFY);
        rectify.setRectifyCode(nextCode);
        rectify.setRectifyStatus("1");
        String jobId = save.getJobId();
        String jobType = save.getJobType();
        if ("daily".equals(jobType)){
            InsDailyJob dailyJob = insDailyJobMapper.getById(jobId);
            String jobName = dailyJob.getJobName();
            rectify.setRectifyName(jobName + "整改");

            InsDailyJob job = new InsDailyJob();
            job.setId(jobId);
            job.setJobStatus(InsJobStatusEnum.RECTIFYING.getStatus()+"");
            insDailyJobMapper.updateIgnoreNull(job);
        }else {
            InsConformJob conformJob = insConformJobMapper.getById(jobId);
            String jobName = conformJob.getJobName();
            rectify.setRectifyName(jobName + "整改");

            InsConformJob job = new InsConformJob();
            job.setId(jobId);
            job.setJobStatus(InsJobStatusEnum.RECTIFYING.getStatus()+"");
            insConformJobMapper.updateIgnoreNull(job);
        }

        insJobRectifyMapper.insert(rectify);
        InsJobError jobError = new InsJobError();
        BeanUtils.copyProperties(rectify,jobError);
        String joberrorId = UUID.randomUUID().toString();
        jobError.setId(joberrorId);
        jobError.setJobRctifyId(rectifyId);
        insJobErrotMapper.insert(jobError);
        //处理异常附件
        List<InsFile> files = save.getFiles();
        for (InsFile file : files) {
            file.setBindId(rectifyId);
            file.setInTime(new Date());
            file.setId(UUID.randomUUID().toString());
            file.setBindType("rectify");
            file.setInCode(save.getRectifyCode());
            file.setInName(save.getRectifyName());
            insFileMapper.insert(file);
            file.setId(UUID.randomUUID().toString());
            file.setBindId(joberrorId);
            file.setBindType("joberror");
            insFileMapper.insert(file);
        }

        return rectifyId;
    }

    @Override
    public void submitRectify(InsJobRectifyDetail detail) {

        String detailId = UUID.randomUUID().toString();
        detail.setId(detailId);
        detail.setSubTime(new Date());
        insJobRectifyDetailMapper.insert(detail);
        if ("Y".equals(detail.getIsFish())){
            String jobRectifyId = detail.getJobRectifyId();
            InsJobRectify rectify = insJobRectifyMapper.getById(jobRectifyId);

            //更新所绑定的任务状态
            String jobType = rectify.getJobType();
            String jobId = rectify.getJobId();
            if ("daily".equals(jobType)){
                InsDailyJob daliyJob = new InsDailyJob();
                daliyJob.setId(jobId);
                daliyJob.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");

                insDailyJobMapper.updateIgnoreNull(daliyJob);
            }else {
                InsConformJob conformJob = new InsConformJob();
                conformJob.setId(jobId);
                conformJob.setJobStatus(InsJobStatusEnum.COMPLIE.getStatus()+"");
                insConformJobMapper.updateIgnoreNull(conformJob);
            }

        }

        List<InsFile> files = detail.getFiles();
        for (InsFile file : files) {
            file.setBindId(detailId);
            file.setInTime(new Date());
            file.setId(UUID.randomUUID().toString());
            file.setBindType("recDetail");
            file.setInCode(detail.getSubWorkerCode());
            file.setInName(detail.getSubWorkerName());
            insFileMapper.insert(file);
        }

    }

    @Override
    public void finishRectify(String recId) {

        InsJobRectify rectify = new InsJobRectify();
        rectify.setId(recId);
        rectify.setRectifyStatus("2");

        insJobRectifyMapper.updateIgnoreNull(rectify);

        InsJobError error = new InsJobError();
        error.setJobRctifyId(recId);
        error.setRectifyStatus("2");

        insJobErrotMapper.updateIgnoreNull(error);
    }
}
